package m3;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;
import java.util.Scanner;

/**
 * 发布订阅模式
 */
public class Producer {
    public static void main(String[] args) throws Exception {
        //连接
        ConnectionFactory f = new ConnectionFactory();
        f.setHost("192.168.64.140");
        f.setPort(5672);
        f.setUsername("admin");
        f.setPassword("admin");
        Channel c = f.newConnection().createChannel();//通信通道
        //定义交换机,命名logs
        //c.exchangeDeclare("logs","fanout")
        c.exchangeDeclare("logs", BuiltinExchangeType.FANOUT);
        //向logs交换机发送消息
        while (true){
            System.out.println("输入");
            String msg = new Scanner(System.in).nextLine();
            //第二个参数对funout交换机无效
            c.basicPublish("logs","", null, msg.getBytes());
        }
    }
}
